#!/bin/bash

###SHELLPACK preamble kvmstart-bench 2.99
###SHELLPACK addon fragment

###SHELLPACK parseargBegin
###SHELLPACK parseargInstall
###SHELLPACK parseargParam   --nr-cpus		KVMSTART_NR_CPUS
###SHELLPACK parseargParam   --min-memory	KVMSTART_MIN_MEMORY
###SHELLPACK parseargParam   --max-memory	KVMSTART_MAX_MEMORY
###SHELLPACK parseargParam   --workload		KVMSTART_WORKLOAD
###SHELLPACK parseargParam   --workload-param	KVMSTART_WORKLOAD_PARAM
###SHELLPACK parseargParam   --iterations	KVMSTART_ITERATIONS
###SHELLPACK parseargParam   --distro		KVMSTART_DISTRO
###SHELLPACK parseargEnd
###SHELLPACK monitor_hooks

FIO_VERSION=$VERSION
echo $KVMSTART_PRETEST_METHOD | grep -q fio
if [ $? -eq 0 ]; then
	###SHELLPACK check_external_install_required fio fio-${VERSION} ${VERSION}
fi

if [ "$KVMSTART_DISTRO" != "" ]; then
	kvm-deploy --no-tune --use-alt-partition --distro $KVMSTART_DISTRO
fi
###SHELLPACK init_complete
if [ $KVMSTART_MIN_MEMORY -gt $KVMSTART_MAX_MEMORY ]; then
	echo Deploy-only configuration complete
	exit 0
fi

echo $KVMSTART_WORKLOAD > $LOGDIR_RESULTS/workload

NR_STEPS=5
MIN_STEPSIZE=$((1048576*1024))
MEMSIZE=$KVMSTART_MIN_MEMORY
MEMSIZE_STEPPING=$(((KVMSTART_MAX_MEMORY-KVMSTART_MIN_MEMORY)/NR_STEPS))
if [ $MEMSIZE_STEPPING -lt $MIN_STEPSIZE ]; then
	MEMSIZE_STEPPING=$MIN_STEPSIZE
fi

ulimit -n 1048576

if [ "$KVMSTART_PRETEST" != "" ]; then
	eval $KVMSTART_PRETEST prepare --method $KVMSTART_PRETEST_METHOD $KVMSTART_PRETEST_PREPARE_ARGS || die "Failed to prepare $KVMSTART_PRETEST $KVMSTART_PRETEST_METHOD"
fi

while [ $MEMSIZE -le $KVMSTART_MAX_MEMORY ]; do
	MEMSIZE_KB=$((MEMSIZE/1024))
	MEMSIZE_GB=$((MEMSIZE_KB/1048576))
	kvm-stop &> /dev/null
	virsh setmaxmem $KVMSTART_KVM_NAME ${MEMSIZE_GB}G --config || die "Failed to configure maximum ${MEMSIZE_GB}g memory size"
	virsh setmem $KVMSTART_KVM_NAME ${MEMSIZE_GB}G --config || die "Failed to configure ${MEMSIZE_GB}g memory size"
	if [ "$KVMSTART_NR_CPUS" != "" ]; then
		virsh setvcpus $KVMSTART_KVM_NAME $KVMSTART_NR_CPUS --config || die "Failed to configure virtual CPUs"
	fi

	###SHELLPACK iteration_begin $KVMSTART_ITERATIONS
		echo Stopping KVM instance
		kvm-stop &> /dev/null

		if [ "$KVMSTART_PRETEST" != "" ]; then
			echo Pretest $KVMSTART_PRETEST_METHOD $KVMSTART_PRETEST_RUN_ARGS
			eval $KVMSTART_PRETEST run --method $KVMSTART_PRETEST_METHOD $KVMSTART_PRETEST_RUN_ARGS || die "Failed to prepare $KVMSTART_PRETEST $KVMSTART_PRETEST_METHOD"
		fi

		echo Timing for memory size ${MEMSIZE_GB}g iteration $ITERATION/$KVMSTART_ITERATIONS
		$TIME_CMD -o $LOGDIR_RESULTS/startup-$MEMSIZE_GB-$ITERATION.time \
			echo o starting KVM
			kvm-start &> $LOGDIR_RESULTS/startup-$MEMSIZE_GB-$ITERATION.log

		KVM_IP=`kvm-ip-address`
		if [ "$KVM_IP" = "" ]; then
			die "Failed to detect IP address of KVM machine"
		fi

		ssh root@$KVM_IP free -m > $LOGDIR_RESULTS/freemem-$MEMSIZE_GB.log
		case $KVMSTART_WORKLOAD in
		memhog)
			echo o post-boot workload $KVMSTART_WORKLOAD
			KVM_MEMSIZE=$((MEMSIZE_KB*KVMSTART_WORKLOAD_PARAM/100))
			$TIME_CMD -o $LOGDIR_RESULTS/memhog-$MEMSIZE_GB.$ITERATION.time	\
				ssh root@$KVM_IP memhog -r3 ${KVM_MEMSIZE}k		\
					&> $LOGDIR_RESULTS/memhog-$MEMSIZE_GB-$ITERATION.log
			;;
		esac
	###SHELLPACK iteration_end $KVMSTART_ITERATIONS

	MEMSIZE=$((MEMSIZE+$MEMSIZE_STEPPING))
done

if [ "$KVMSTART_PRETEST" != "" ]; then
	$KVMSTART_PRETEST cleanup --method $KVMSTART_PRETEST_METHOD
fi
exit $SHELLPACK_SUCCESS
